using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;

namespace WebService.Client
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            DataGridViewColumn col = new DataGridViewColumn();
            DataGridViewCell cell = new DataGridViewTextBoxCell();
            col.Name = "Logins";
            col.DataPropertyName = "login";
            col.ValueType = "".GetType();
            col.HeaderText = "login";
            col.CellTemplate = cell;
            dataGridView1.Columns.Add(col);
            Admin.AdminClient client = new Admin.AdminClient();
            client.ClientCredentials.UserName.UserName = "register";
            client.ClientCredentials.UserName.Password = "register";
            client.Open();

            BindingList<Admin.CasinoUser> list = client.GetAllUsers();
            client.Close();
            dataGridView1.DataSource = list;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.DefaultExt = "*.jpg";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                txtFile.Text = dialog.FileName;
                pictureBox1.Image = new Bitmap(dialog.FileName, true);
                AvatarOK = true;
            }
            else
                AvatarOK = false;

        }

        private bool AvatarOK = false;

        private void btnCreate_Click(object sender, EventArgs e)
        {
            Admin.CasinoUser casinoUsr = new WebService.Client.Admin.CasinoUser();
            casinoUsr.user_name = txtName.Text;
            casinoUsr.user_pass = txtPass.Text;
            casinoUsr.user_surname = txtSurname.Text;
            casinoUsr.nick = txtNick.Text;
            casinoUsr.login = txtlogin.Text;
            casinoUsr.email = txtemail.Text;
            casinoUsr.country = txtcountry.Text;
            casinoUsr.birth_date = dateTimePicker1.Value;
            if (AvatarOK)
                casinoUsr.avatar = System.IO.File.ReadAllBytes(txtFile.Text);
            else
                casinoUsr.avatar = null;
             Admin.AdminClient client = null;
            try
            {
                client = new WebService.Client.Admin.AdminClient();
                client.ClientCredentials.UserName.UserName = "register";
                client.ClientCredentials.UserName.Password = "register";
                client.Open();
                Admin.MembershipCreateStatus status = client.AddUser(casinoUsr);
                client.Close();
                MessageBox.Show("Status : "+status.ToString());
            }
            catch (Exception ex)
            {
                if (client != null && client.State == System.ServiceModel.CommunicationState.Opened)
                    client.Close();
                MessageBox.Show(ex.Message);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            Admin.AdminClient client = null;

            try
            {
                client = new WebService.Client.Admin.AdminClient();
                client.ClientCredentials.UserName.UserName = txtLoginLogin.Text;
                client.ClientCredentials.UserName.Password = txtLoginPassword.Text;
                client.Open();
                int Money = 0;
                Admin.CasinoUser usr = client.Login(txtLoginLogin.Text, out Money);
                client.Close();
                if (usr != null)
                {
                    txtName.Text=usr.user_name;
                    txtPass.Text=usr.user_pass; 
                    txtSurname.Text=usr.user_surname; 
                    txtNick.Text=usr.nick; 
                    txtlogin.Text=usr.login; 
                    txtemail.Text=usr.email; 
                    txtcountry.Text=usr.country; 
                    if(usr.birth_date.HasValue)
                    dateTimePicker1.Value=usr.birth_date.Value; 
                  
                    byte[] imagecontent =  System.IO.File.ReadAllBytes("C:\\avatar3.gif");                  
                    ///

                    pictureBox1.Image = ConvertByteArrayToImage(imagecontent);
                }
                else
                    MessageBox.Show("Login failed");
            }
            catch (Exception ex)
            {
                if (client != null && client.State == System.ServiceModel.CommunicationState.Opened)
                    client.Close();
                MessageBox.Show(ex.Message);
            }
        }

        public Image ConvertByteArrayToImage(byte[] byteArray)
        {
            if (byteArray != null)
            {
                MemoryStream ms = new MemoryStream(byteArray, 0,
                byteArray.Length);
                ms.Write(byteArray, 0, byteArray.Length);
                return Image.FromStream(ms, true);
            }
            return null;
        }
        public byte[]ConvertImageToByteArray(System.Drawing.Image imageToConvert,ImageFormat formatOfImage)
        {
            byte[] Ret;
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    imageToConvert.Save(ms, formatOfImage);
                    Ret = ms.ToArray();
                }
            }
            catch (Exception) { throw; }
            return Ret;
        }
    }
}